import D2Ext

from .CImage import CImage
from .CLabel import CLabel


class CCheckBox(CImage):

    def __init__(self, parent=None):
        super(CCheckBox, self).__init__(parent)
        self.frame_unchecked = 0
        self.frame_checked = 1
        self.text = CLabel(self)
        self.text_x = self.text.x
        self.text_y = self.text.y
        self.space = 8
        self.checked = False
        self.connect(self.SIGNAL_ONCLICK, self.on_click)

    def set_check_frame(self, unchecked, checked):
        self.frame_unchecked = unchecked
        self.frame_checked = checked
        self.set_frame(unchecked)

    def set_text(self, text):
        self.text.set_text(text)

        # D2Ext.LogMsg("self.x=%s,self.y=%s,self.width=%s,self.height=%s" % (self.x, self.y, self.width, self.height))
        # D2Ext.LogMsg("t_x=%s,t_y=%s" % (t_x, t_y))

    def set_position(self, x, y):
        super(CCheckBox, self).set_position(x, y)
        self.text_x = self.dx + self.width + self.space
        t_height = 12
        self.text_y = self.dy + (self.height - t_height) / 2 + t_height
        self.text.set_position(self.text_x, self.text_y)

    def draw(self):
        if self.checked:
            self.set_frame(self.frame_unchecked)
        else:
            self.set_frame(self.frame_checked)
        D2Ext.Image_Draw(self.image_file, self.dx, self.dy + self.height, self.opacity)

    def show(self):
        super(CCheckBox, self).show()
        self.text.show()

    def hide(self):
        super(CCheckBox, self).hide()
        self.text.hide()

    def on_click(self):
        self.checked = not self.checked
